/*!
 * 我的图文选择模块
 * @author Sherlock 
 * ---------------------------
 * mpnewsChooseCallback图文选择确认后的回调
 */
require( './style.less' );
define( [ 'jquery', 'require', 'bootstrap', 'message', 'blocksit', 'api', 'bigwe_utils' ], function( $ ) {
  var returnParam = {};
  var ShowTitleMessage = require( 'message' );
  var bu = require( 'bigwe_utils' );
  var showTime = ShowTitleMessage.showTime;
  var BIGWE_API = require( 'api' );

  var wechartImgCheck = bu.wechartImgCheck;

  var $me = returnParam.$selector = $( '#my_mpnews' ),
    Mpnews = {};
  var queryParam = {
    'key': '',
    'custom_group_id': '',
    'p': 1,
    'l': 10
  }
  var isLoading = 0,
    isNoData = 0;

  var result = [];
  $me.on( 'shown.bs.modal', function() {
    $( this ).show();
    $( '#mpnew_list', $me ).BlocksIt( 'reload' );
    if ( $( '#mpnew_list .HeC', this ).length ) return;
    if ( !Mpnews.firstLoad ) {
      Mpnews.firstLoad = !0;
      ajaxRequest();
    }
  } ).on( 'click', '.search_btn', function() {
    Mpnews.pageRest = 0;
    $.extend( queryParam, {
      p: 1,
      key: $( '#my_mpnews .search_input' ).val()
    } )
    ajaxRequest();
  } ).on( 'click', '.height_cacu button', function() {
    $( this ).addClass( 'selected' ).siblings().removeClass( 'selected' );
    $.extend( queryParam, {
      p: 1,
      custom_group_id: $( this ).attr( 'group_id' ) || ''
    } );
    Mpnews.pageRest = 0;
    ajaxRequest();

  } ).on( 'click', '#mpnews_sure', function() {

    var selected = $( '.js_HeC.selected', $me );

    if ( selected.length ) {

      if ( typeof returnParam.mpnewsChooseCallback === 'function' ) {
        returnParam.mpnewsChooseCallback.call( $me, {
          'groupid': selected.attr( 'groupid' ) || '',
          'mpnewsArray': result[ selected.index() ],
          'mpnewsHtml': selected.prop( 'outerHTML' )
        } );
      }
      $me.modal( 'hide' );
    } else {
      ShowTitleMessage.showWarning( '请选择一个图文', showTime );
    }
  } ).on( 'click', '.more', function() {
    if ( $( this ).hasClass( 'showmore' ) ) {
      $( this ).removeClass( 'showmore' );
      $( '.group_list', $me ).height( 28 );
    } else {
      $( this ).addClass( 'showmore' );
      $( '.group_list', $me ).height( $( '.height_cacu', $me ).height() );
    }
  } );

  $( '#mpnew_list', $me ).BlocksIt( {
    numOfCol: 3,
    offsetX: 4,
    offsetY: 5
  } ).on( 'click', '.js_HeC', function() {
    $( this ).addClass( 'selected' ).siblings().removeClass( 'selected' );
  } );

  function pageRander( total ) {
    $( '.pageControl', $me ).bwPagination( {
      total: total,
      pageCount: queryParam.l,
      autoVisibility: !0,
      callback: function( index ) {
        $.extend( queryParam, {
          p: index
        } );
        ajaxRequest();
      }
    } );
  }

  function ajaxRequest( fn ) {
    isLoading = 1;
    $( '.spinner', $me ).show();
    $( '#mpnew_list', $me ).empty()
    $.ajax( {
      type: 'post',
      url: BIGWE_API.MYMPNEWS.GET_FULL_MSG,
      data: queryParam,
      success: function( data ) {
        data = typeof data === 'string' ? $.parseJSON( data ) : data;
        if ( !data.ret ) {
          let detailData = data.data;

          if ( !Mpnews.pageRest ) {
            Mpnews.pageRest = !0;
            pageRander( detailData.total );
          }

          if ( !Mpnews.groupLoad ) {
            Mpnews.groupLoad = !0;
            var groupHtml = '';
            for ( let i in detailData.customs ) {
              var group = detailData.customs[ i ];
              groupHtml += '<button type="button" group_id="' + group.id + '">' + group.group_name + '</button>';
            }
            var cacuHieight = $( '.height_cacu', $me ).append( groupHtml ).height();
            if ( cacuHieight > 34 ) {
              $( '.more', $me ).show();
            }
          }

          if ( !detailData.list.length ) {
            ShowTitleMessage.showWarning( '没有查询到数据', showTime );
            return;
          }
          result = detailData.list;
          var appendHtml = '';

          for ( let i in detailData.list ) {
            appendHtml += getMpNewsHtml( detailData.list[ i ] )
          }

          $( '#mpnew_list', $me ).html( appendHtml ).BlocksIt( 'reload' );
        }

        if ( fn ) {
          fn.call( $me );
        }

      },
      complete: function() {
        isLoading = 0;
        $( '.spinner', $me ).hide();
      }
    } );
  }

  function getMpNewsHtml( graphArray ) {

    var isIntact = isIntactGraph( graphArray );
    var html = '';

    if ( graphArray.articles && !graphArray.articles.length ) return html;

    html += '<div class="HeC' + ( isIntact ? ' js_HeC' : '' ) + '" groupid="' + graphArray.article_group + '">';
    if ( isIntact ) {
      html += '<div class="mask"><span class="glyphicon glyphicon-ok ok_icon"></span></div>';
    }
    html += '<div class="outer_content">';
    html += '<div class="HeC_1">';
    if ( graphArray.create_time ) {
      html += '<p class="upload_time">' + graphArray.create_time + '</p>';
    }

    if ( graphArray.articles.length == 1 || !graphArray.articles[ 0 ].cover ) {
      html += '<a class="title">' + ( graphArray.articles[ 0 ].title || '' ) + '</a>';
    }

    if ( graphArray.articles[ 0 ].cover ) {

      html += '<div class="HeC_1_inter">';
      html += '<img class="cover" src="' + wechartImgCheck( graphArray.articles[ 0 ].cover ) + '"/>';

      if ( graphArray.articles.length > 1 ) {
        html += '<a target="_blank" class="graph_gray">' + graphArray.articles[ 0 ].title + '</a>';
      }
      html += '</div>';
    }

    if ( graphArray.articles.length == 1 ) {
      html += '<div class="desc">' + ( graphArray.articles[ 0 ].summary || '' ) + '</div>';
    }

    html += '</div>';
    html += '<div class="content">';
    html += '<table>';
    html += '<tbody>';

    for ( i in graphArray.articles ) {
      if ( i != '0' ) {
        html += '<tr><td>';
        html += '<a target="_blank">' + graphArray.articles[ i ].title + '</a>';
        html += '</td><td>';
        if ( graphArray.articles[ i ].cover ) {
          html += '<img class="cover_right" src="' + wechartImgCheck( graphArray.articles[ i ].cover ) + '"/>';
        }
        html += '</td></tr>';
      }
    }

    html += '</tbody>';
    html += '</table>';
    html += '</div>';

    if ( !isIntact ) {
      html += '<div class="noIntact_mask">';
      html += '<p>图文不完整，无法选中，<a href="/edit_text?article_group=' + graphArray.article_group + '" target="_blank">继续编辑</a></p>'
      html += '</div>';
    }

    html += '</div>';
    html += '</div>';

    return html;

  }

  function isIntactGraph( graphObj ) {
    if ( !graphObj.is_complete ) return false;

    for ( let i in graphObj.articles ) {
      var article = graphObj.articles[ i ];
      if ( !article.title || !article.cover ) {
        return false;
      }
    }
    return true;
  }


  return returnParam;
} );
